package com.voltmao.service.application.doc;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.voltmao.persistent.mapper.DocTypeMapper;
import com.voltmao.persistent.model.DocType;
import com.voltmao.persistent.model.DocTypeCriteria;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.utils.ParamsUtil;
import com.voltmao.webpro.utils.PropertiesUtils;
import com.voltmao.webpro.utils.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DocTypeServiceImpl {
    @Autowired
    DocTypeMapper docTypeMapper;

    /**
     * 新增或更新文档分类
     * @param params
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdate(Map<String, Object> params) throws RuntimeException{
        //检查参数完整性
        String [] flag = {"flag"};
        ParamsUtil.checkNotNull(params,flag);
        String tmpFlag = params.get("flag").toString();
        //新增
        if(tmpFlag.equals("INSERT")){
            String[] checkParams = {"name","typeDes","forCorpEmployesUser"};
            ParamsUtil.checkNotNull(params,checkParams);
        }
        //更新
        if(tmpFlag.equals("UPDATE")){
            String[] checkParams = {"dataId"};
            ParamsUtil.checkNotNull(params,checkParams);
        }
        //删除
        if(tmpFlag.equals("DELETE")){
            String[] checkParams = {"dataId","isDel"};
            ParamsUtil.checkNotNull(params,checkParams);
        }

        DocType docType = (DocType)PropertiesUtils.mapToObject(params,DocType.class);
        if(StringUtils.isEmpty(docType.getDataId())){
            docType.setCreateDate(new Date());
            docType.setDataId(StringUtils.uuid36());
            docType.setIsDel(0);
            return docTypeMapper.insert(docType);
        }else{
            docType.setUpdateDate(new Date());
            return  docTypeMapper.updateByPrimaryKeySelective(docType);
        }
    }

    public DataPage getDocType(Map<String, Object> params){
        DataPage dataPage = new DataPage();
        int pageIdx = StringUtils.parseInt(params.get("pageNumber") + "", 1);
        int pageSize = StringUtils.parseInt(params.get("pageSize") + "", 20);

        DocTypeCriteria e = new DocTypeCriteria();
        DocTypeCriteria.Criteria c = e.createCriteria();
        e.setPageSize(pageSize);
        e.setLimitStart((pageIdx - 1) * pageSize);
        //类型名
        if(StringUtils.isNotEmpty(params.get("name"))){
            c.andNameLike("%" + params.get("name") + "%");

        }
        c.andIsDelEqualTo(0);
        e.setOrderByClause("pid desc");
        dataPage.pageindex = pageIdx;
        dataPage.pagesize = pageSize;
        dataPage.rows = docTypeMapper.selectByExample(e);
        dataPage.total = docTypeMapper.countByExample(e);
        return dataPage;
    }

    public List<DocType> getAllDocType(){
        DocTypeCriteria e = new DocTypeCriteria();
        DocTypeCriteria.Criteria c = e.createCriteria();
        c.andIsDelEqualTo(0);
        return  docTypeMapper.selectByExample(e);
    }
}
